package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import cn.edu.nuc.onlinestore.model.Cart;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.network.TCPClient;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;

public class UserGoods extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JFrame jf;
	private static String username;
	private int buyGoodscount = 0;
    private Goods goods=new Goods();;
	/**
	 * 
	 * Launch the application.
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { UserGoods frame = new UserGoods();
	 * frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
	 * }); }
	 */
	/**
	 * Create the frame.
	 */
	public UserGoods() {

	}

	public int getBuyGoodscount() {
		return buyGoodscount;
	}

	public void setBuyGoodscount(int buyGoodscount) {
		this.buyGoodscount = buyGoodscount;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public UserGoods(List<String> list, int NO,int row,DefaultTableModel model,String rowValue) {

		this.jf = this;
		String str = "Goods"; // list.iterator()
		for (int i = 0; i < list.size(); i++) {
			str = str + "," + list.get(i);
		}
		String[] goodsInfo = str.split(",");

		setTitle("商品详情");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 405);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("商品名称:");
		label.setBounds(23, 57, 66, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("单价:");
		label_1.setBounds(35, 100, 54, 15);
		contentPane.add(label_1);

		JLabel label_2 = new JLabel("单位:元");
		label_2.setBounds(321, 100, 54, 15);
		contentPane.add(label_2);

		JLabel label_4 = new JLabel("简介:");
		label_4.setBounds(35, 141, 44, 46);
		contentPane.add(label_4);

		JLabel lblNewLabel = new JLabel(goodsInfo[2]);
		lblNewLabel.setBounds(99, 57, 162, 15);
		contentPane.add(lblNewLabel);

		JLabel label_3 = new JLabel(goodsInfo[3]);
		label_3.setBounds(99, 100, 54, 15);
		contentPane.add(label_3);

		JLabel lblNewLabel_1 = new JLabel(goodsInfo[4]);
		lblNewLabel_1.setBounds(99, 141, 251, 46);
		contentPane.add(lblNewLabel_1);

		textField = new JTextField();

		textField.setBounds(99, 244, 66, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("购买数量:");
		lblNewLabel_2.setBounds(35, 247, 71, 15);
		contentPane.add(lblNewLabel_2);

		JLabel label_5 = new JLabel("库存: " + goodsInfo[5]);
		label_5.setBounds(99, 273, 135, 15);
		contentPane.add(label_5);

		JButton button = new JButton("加入购物车");
		button.setBounds(175, 243, 126, 23);
		contentPane.add(button);

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// int count=Integer.parseInt(textField.getText());
				String msg = "buy###" + String.valueOf(NO) + "###" + textField.getText().trim() + "###"
						+ UserGoods.username;
				String result = new TCPClient().send(msg);
              
				if ("success".equals(result.split("###")[0])) {

					JOptionPane.showConfirmDialog(null, "您购买" + textField.getText() + "件该商品！");
				
					//model.setValueAt(Integer.parseInt(rowValue), row-1,3 );
					int shopcount=Integer.parseInt(result.split("###")[1]);
//				    UserStore us=new UserStore();
//				    us.setTotalCount(shopcount);
					UserStore.totalCount+=shopcount;
					    
					jf.dispose();
                  
				} else {
					JOptionPane.showMessageDialog(null, result);
				}

			}

		});
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				((UserGoods) e.getSource()).setVisible(false);
				
			}
		});
	}
}
